#!/bin/bash
#rm catalog.html catalog.txt >/dev/null 2>&1
echo "<html>"$'\n'"<head>"$'\n''<meta charset="utf-8">' > catalog.txt
if [ -n "$1" ] && [[ "$1" == *.css ]]
then
	echo '<link rel="stylesheet" type="text/css" href="'"$1"'">' >> catalog.txt
fi
echo "</head>"$'\n'"<body>" >> catalog.txt
if [ ! -z "$(ls *.html 2>&1 >/dev/null)" ]
then
	exit
fi
for loop in `ls -t *.html`
do
	title="$(cat "$loop" | grep "<h1>" | sed -n 1p)"
	if [ ! -z "$title" ]
	then
		title="${title#*<h1>}"
		title="${title%</h1>*}"
		echo '<a href="'"$loop"'" target="_blank">'"$title</a>"$'\n'"<br />" >> catalog.txt
	fi
done
echo "</body>"$'\n'"</html>" >> catalog.txt
mv catalog.txt catalog.html
